import { CallHandler, Injectable, NestInterceptor } from "@nestjs/common";
import { map } from 'rxjs/operators'
import { Observable } from "rxjs";
interface Data<T> {
    data: T
}

// 响应封装
@Injectable()
export class Response<T> implements NestInterceptor {
    intercept(context, next: CallHandler): Observable<Data<T>> {
        const request = context.switchToHttp().getRequest() as Request;

          // 生成唯一的 requestId
        return next.handle().pipe(map(data => {
            const isErrorResponse = data && data.errors;
            return {
                data,
                code:isErrorResponse ? -1 : 0,
                requestId:request['requestId'],
                message:isErrorResponse?.message || "success!"
            }
        }))
      
    }
}
